package com.kefu.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.sys.model.SysUser;
import com.kefu.sys.service.SysUserService;
import com.kefu.sys.vo.SysUserSearchVo;
import com.kefu.sys.vo.SysUserBatchUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@Api(tags = "用户")
@RestController
@RequestMapping("sys/user")
public class SysUserController extends AbstractBaseController {
    @Resource
    private SysUserService sysUserService;

    @PostMapping("/list")
    @ApiOperation(value = "查询用户", notes = "查询用户")
    public Result<Page<SysUser>> list(@Validated @RequestBody SysUserSearchVo search) {
        return ResultGenerator.genSuccessResult(sysUserService.search(search));
    }

    @PostMapping("/batchUpdate")
    @ApiOperation(value = "批量修改用户", notes = "批量修改用户")
    public Result<String> batchUpdate(@Validated @RequestBody SysUserBatchUpdateVo vo) {
        logger.info("批量修改用户:{}", vo);
        vo.getUserIdList().forEach(userId ->{
            SysUser info = new SysUser();
            info.setUserId(userId);
            if (StrUtil.isNotBlank(vo.getPassword())) {
                info.setUserPwd((new BCryptPasswordEncoder()).encode(vo.getPassword()));
            }
            if (vo.getUserStatus()!=null){
                info.setUserStatus(vo.getUserStatus());
            }
            if (vo.getExpireTime()!=null){
                info.setExpireTime(vo.getExpireTime());
            }
            if (vo.getCallTimeout()!=null){
                info.setCallTimeout(vo.getCallTimeout());
            }
            sysUserService.update(info);
        });
        return ResultGenerator.genSuccessResult();
    }

}
